package com.leetcode.algorithm.y22.m07.w3;

/**
 * 66. 加一
 * 
 * https://leetcode.cn/problems/plus-one/
 * 
 * @author jie.deng
 *
 */
class Question0066Solution01 {
	
	public int[] plusOne(int[] digits) {
		int len = digits.length;
		int carry = 0;
		for (int i = len - 1; i >= 0; i--) {
			int sum = digits[i] + ((i == len - 1) ? 1 : 0) + carry;
			digits[i] = sum % 10;
			carry = sum / 10;
		}
		if (carry == 0) {
			return digits;
		}
		int[] arr = new int[len + 1];
		arr[0] = 1;
		for (int i = 0; i < arr.length; i++) {
			arr[i + 1] = digits[i];
		}
		return arr;
	}
    
}